package com.sokal.easycare.modules.contract.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.contract.entity.ContractTemplate;
import com.sokal.easycare.modules.contract.service.ContractTemplateService;

/**
 * 合同模板Controller
 * @author liliangdong
 * @version 2017-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/contract/contractTemplate")
public class ContractTemplateController extends BaseController {

	@Autowired
	private ContractTemplateService contractTemplateService;
	
	@ModelAttribute
	public ContractTemplate get(@RequestParam(required=false) String id) {
		ContractTemplate entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = contractTemplateService.get(id);
		}
		if (entity == null){
			entity = new ContractTemplate();
		}
		return entity;
	}
	
	@RequiresPermissions("contract:contractTemplate:view")
	@RequestMapping(value = {"list", ""})
	public String list(ContractTemplate contractTemplate, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ContractTemplate> page = contractTemplateService.findPage(new Page<ContractTemplate>(request, response), contractTemplate); 
		model.addAttribute("page", page);
		return "modules/contract/contractTemplateList";
	}

	@RequiresPermissions("contract:contractTemplate:view")
	@RequestMapping(value = "form")
	public String form(ContractTemplate contractTemplate, Model model) {
		model.addAttribute("contractTemplate", contractTemplate);
		return "modules/contract/contractTemplateForm";
	}

	@RequiresPermissions("contract:contractTemplate:edit")
	@RequestMapping(value = "save")
	public String save(ContractTemplate contractTemplate, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, contractTemplate)){
			return form(contractTemplate, model);
		}
		contractTemplateService.save(contractTemplate);
		addMessage(redirectAttributes, "保存合同模板成功");
		return "redirect:"+Global.getAdminPath()+"/contract/contractTemplate/?repage";
	}
	
	@RequiresPermissions("contract:contractTemplate:edit")
	@RequestMapping(value = "delete")
	public String delete(ContractTemplate contractTemplate, RedirectAttributes redirectAttributes) {
		contractTemplateService.delete(contractTemplate);
		addMessage(redirectAttributes, "删除合同模板成功");
		return "redirect:"+Global.getAdminPath()+"/contract/contractTemplate/?repage";
	}

}